<template>
  <view class="wrap">
    <view class="img">
      <image src="/static/images/img-7.png"></image>
    </view>
    <view class="title">小剂将因你变的更好</view>
    <view class="btn" @click="chooseImage">
      拍照纠错
    </view>
  </view>
</template>

<script>
import { HTTP_REQUEST_URL } from '@/config/app.js'
import { setFeedback } from '@/api/api.js'
export default {
  data() {
    return {
      submitMain: false,
      retImg: '',
      context: null,
      code: ''
    };
  },
  onLoad(opition) {
    if (opition.type) {
      this.type = opition.type
      if (opition.type == 1) {
        uni.setNavigationBarTitle({
          title: "上报补充信息"
        })
      } else {
        uni.setNavigationBarTitle({
          title: "产品纠错"
        })
      }

    }
    if (opition.code) {
      this.code = opition.code
    }
  },
  methods: {
    error(e) {
      console.log(e);
    },
    toIndex() {
      uni.navigateTo({
        url: '/pages/tabbar/index'
      })
    },
    chooseImage() {
      const that = this
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        success: (res) => {
          that.urlArr = []
          that.uploadFileAdd(res, 0, res.tempFilePaths.length)
        }
      });
    },
    async uploadFileAdd(res, num, max) {
      const that = this
      if (num == max) {
        that.$nextTick(() => {
          that.setRestockItemFun(that.urlArr.join())
        })
      } else {
        const result = await that.uploadFileFun(res.tempFilePaths[num])
        that.urlArr.push(result.data.url)
        this.uploadFileAdd(res, num + 1, max)
      }

    },
    async uploadFileFun(src) {
      let token = uni.getStorageSync('LOGIN_STATUS_TOKEN')

      return new Promise((resolve, reject) => {
        uni.uploadFile({
          url: 'https://app.jichacha.vip/api/newUpload',
          filePath: src,
          name: "file",
          header: {
            'Authorization': 'Bearer ' + token
          },
          success(res1) {
            resolve(JSON.parse(res1.data))
          },
          fail(res) {
            reject(res)
          }
        })
      })
    },
    setRestockItemFun(url) {
      const that = this
      //进行照相
      setFeedback({
        type: that.type == 1 ? 1 : 0,
        content_img: url,
        gcode: that.code
      }).then((resobj) => {
        uni.showToast({
          title: '提交成功',
          icon: 'none'
        });
      }).catch((err) => {
        uni.showToast({
          title: err,
          icon: 'none'
        });
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.wrap {
  width: 750rpx;
  min-height: 100vh;
  position: relative;
  background-color: white;
  .img {
    width: 177rpx;
    height: 193rpx;
    margin: 150rpx auto 0 auto;
    overflow: hidden;
    image {
      width: 100%;
      height: 100%;
      display: inline-block;
      vertical-align: top;
    }
  }
  .title {
    color: #434242;
    font-size: 34rpx;
    text-align: center;
    font-weight: 700;
    line-height: 80rpx;
    margin: 50rpx 0;
  }
  .btn {
    line-height: 100rpx;
    width: 500rpx;
    text-align: center;
    color: white;
    background-color: #2767e6;
    border-radius: 50rpx;
    font-weight: 700;
    font-size: 30rpx;
    margin: 100rpx auto;
  }
}
</style>
